package conexion.servidor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import persistencia.consultas.Consultas;
import valueObjects.VODuenio;
import logica.excepciones.PersistenciaException;
import logica.fachada.Fachada;



public class MainServidor {

	public static void main(String[] args) throws PersistenciaException {

		try{ // pongo a correr el rmiregistry
			LocateRegistry.createRegistry(1099);
			// instancio mi Objeto Remoto y lo publico
			Fachada fachada = Fachada.getInstancia();
			Properties p = new Properties();
		    String nomArch = "config/datos.properties";
		    p.load(new FileInputStream(nomArch));
		    String ip = p.getProperty("ip");
			String puerto = p.getProperty("port");
			int port = Integer.parseInt(puerto);
			LocateRegistry.createRegistry(port);
			String ruta = "//" + ip + ":" + puerto + "/fachada";
			Naming.rebind(ruta, fachada);
			System.out.println("Publicado");
			
		} catch (RemoteException e){		
			e.printStackTrace();
		} catch (MalformedURLException e){
			throw new PersistenciaException(e.getMessage());
		} catch (FileNotFoundException e) {
			throw new PersistenciaException(e.getMessage());
		} catch (IOException e) {
			throw new PersistenciaException(e.getMessage());
		}
				
	}

}

/*

//prueba son usar rmi
VODuenio voDue = new VODuenio(12345, "benja", "abreo");
facade.nuevoDuenio(voDue);*/


	/*Connection con = null;
	
	try {
		Class.forName(driver);
		con = DriverManager.getConnection(url, user, password);
		Statement stmt;

		Consultas c = new Consultas();
		String crearDB = c.crearBD();
		String crearDuenios = c.crearTablaDuenio();
		String crearMascotas = c.crearTablaMascotas();
		String insertar1 = c.insertar1();
		String insertar2 = c.insertar2();
		String insertar3 = c.insertar3();
		String insertar4 = c.insertar4();
		String insertar5 = c.insertar5();
		
		stmt = con.createStatement();
		stmt.executeUpdate(crearDuenios);

		stmt = con.createStatement();
		stmt.executeUpdate(crearMascotas);

		stmt = con.createStatement();
		stmt.executeUpdate(insertar1);
		stmt = con.createStatement();
		stmt.executeUpdate(insertar2);
		stmt = con.createStatement();
		stmt.executeUpdate(insertar3);
		stmt = con.createStatement();
		stmt.executeUpdate(insertar4);
		stmt = con.createStatement();
		stmt.executeUpdate(insertar5);

		PreparedStatement pstmt = con.prepareStatement(crearDB);
		pstmt.executeUpdate();
		
		stmt.close();
		pstmt.close();
		con.close();
	} catch (ClassNotFoundException | SQLException e) {
		e.printStackTrace();
	}
	*/
